package com.group.app.example.controller;

import com.alibaba.fastjson.JSON;
import com.group.app.example.common.support.ApiResponse;
import com.group.app.example.service.FieldService;
import com.group.app.example.util.DownloadUtil;
import com.group.app.example.vo.FieldWarehouseVo;
import com.group.app.example.vo.SearchVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/field/management")
@Validated
public class FieldController {

    @Autowired
    FieldService fieldService;

    @RequestMapping("/excel/lead/in")
    public ApiResponse<Map<String,Object>> saveFieldWarehouse(@RequestBody @Valid  List<FieldWarehouseVo> fieldWarehousevos, @RequestParam(value = "user")String user, BindingResult errors){
        ApiResponse<Map<String,Object>> res=new ApiResponse<>();
        fieldService.saveFieldWarehouse(fieldWarehousevos,user);
        res.setCode(200);
        res.setMsg("success");
        return res;
    }

    @RequestMapping("/find/condition")
    public ApiResponse<Map<String,Object>> findFieldByCondition(@RequestBody SearchVo searchVo){
        ApiResponse<Map<String,Object>> res=new ApiResponse<>();
        Map<String, Object> fieldByCondition = fieldService.findFieldByCondition(searchVo);
        res.setCode(200);
        res.setMsg("success");
        res.setData(fieldByCondition);
        return res;
    }

    @RequestMapping("/download/excel")
    public void downloadFieldByCondition(@RequestBody SearchVo searchVo, HttpServletResponse response){
        String fileName="标签库";
        List<Map<String,Object>> list=null;
        try {
            fileName = URLEncoder.encode(fileName, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        try {
            response.addHeader("content-disposition","attachment;filename="+fileName+".xlsx");
            list=fieldService.allLabelInfoByCondition(searchVo);
        }catch (Exception e){

        }
        List<FieldWarehouseVo> data=new LinkedList<>();
        for (Map map:list){
            String string= JSON.toJSONString(map);
            FieldWarehouseVo fieldWarehouseVo = JSON.parseObject(string, FieldWarehouseVo.class);
            data.add(fieldWarehouseVo);
        }
        DownloadUtil.excelDownload(response,data,FieldWarehouseVo.class);

    }


}
